package flexo.physic;

import java.util.LinkedList;
import java.util.List;

public class ContactResolver
{
	public static final int MAX_ITERATIONS = 3;
	protected int iterations;
	
	protected List<Contact> contacts;
	
	public ContactResolver()
	{
		contacts = new LinkedList<Contact>();
	}
	
	void addContact(Contact c)
	{
		contacts.add(c);
	}
	
	public void resolveContacts(float duration)
	{		
		iterations = 0;
		
		if (contacts.size() > 0)
		{
			while (iterations < MAX_ITERATIONS)
			{
				float maxVelocity = 0;
				Contact maxContact = null;
				
				for (Contact c: contacts)
				{
					float vel = c.calculateSeparatingVelocity();
					if (vel < maxVelocity)
					{
						maxVelocity = vel;
						maxContact = c;
					}
				}
				
				if (maxContact != null)
				{
					maxContact.resolve(duration);
				}
				
				++iterations;
			}
		}
		contacts.clear();
	}
}
